package com.example.demo.msg.netty.server;

import com.example.demo.msg.netty.common.IMconfig;
import com.example.demo.msg.netty.common.Imessage;
import com.example.demo.msg.netty.common.MsgPackDecode;
import com.example.demo.msg.netty.common.MsgPackEncode;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;

import java.io.IOException;
import java.util.Scanner;

public class NettyuServer implements Runnable,IMconfig {

    private ServerHandler serverHandler = new ServerHandler();

    public static void main(String[] args) {
        try {
            CompositeByteBuf byteBufs = Unpooled.compositeBuffer();
            new NettyuServer().start();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }

    public void start () throws  IOException{
        new Thread(this).start();
        runServerCMD();

    }

    private void runServerCMD() throws IOException {
        //服务端主动推送消息
        int toID = 1;
        Imessage message = new Imessage(
                APP_IM,
                CLIENT_VERSION,
                SERVER_ID,
                TYPE_MSG_TEXT,
                toID,
                MSG_EMPTY);
        @SuppressWarnings("resource")
        Scanner scanner = new Scanner(System.in);
        do{
            message.setMsg(scanner.nextLine());
        }
        while (serverHandler.sendMsg(message,null));
    }


    @Override
    public void run() {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();

            serverBootstrap.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG,1204)
                    .childOption(ChannelOption.SO_KEEPALIVE,true)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {

                            ChannelPipeline pipeline = socketChannel.pipeline();
                            pipeline.addLast("frameDecoder", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
                            pipeline.addLast("msgPack decoder",new MsgPackDecode());
                            pipeline.addLast("msgPack encoder",new MsgPackEncode());
                            pipeline.addLast(serverHandler);

                        }
                    });

            ChannelFuture f = serverBootstrap.bind(SERVER_PORT).sync();
            f.channel().closeFuture().sync();
        } catch (InterruptedException e) {

            e.printStackTrace();
        }finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }

    }
}